package com.digital.herbarium.Util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TimeUtil {

    public static String getNow() {
        // 当前Date格式时间
        Date date = DateUtil.date();
        // 格式化为日期时间格式字符串
        String formatDateTime = DateUtil.formatDate(date);//结果：2020-07-16 20:22:54
        return formatDateTime;

    }


    public static String getNowTime() {
        // 当前Date格式时间
        Date date = DateUtil.date();
        // 格式化为日期时间格式字符串
        String formatDateTime = DateUtil.formatDateTime(date);//结果：2020-07-16 20:22:54
        return formatDateTime;

    }

    public static String getYesterday() {
        DateTime yesterday = DateUtil.yesterday();
        String yesterdayStr = DateUtil.formatDate(yesterday);
        return yesterdayStr;
    }


    public static List<String> sortDate(List<String> dateStrings) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<String> sortedDateStrings = new ArrayList<>();
        try {
            List<Date> dates = new ArrayList<>();
            for (String dateString : dateStrings) {
                Date date = format.parse(dateString);
                dates.add(date);
            }

            Collections.sort(dates, new Comparator<Date>() {
                public int compare(Date d1, Date d2) {
                    return d1.compareTo(d2);
                }
            });


            for (Date date : dates) {
                String dateString = format.format(date);
                sortedDateStrings.add(dateString);
            }
            return  sortedDateStrings;

           // System.out.println(sortedDateStrings);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return sortedDateStrings;

    }


    public static List<String> getDayList() {
        List<String> list = new ArrayList<>();
        DateTime dateTime = DateUtil.beginOfMonth(DateUtil.yesterday());
        while (dateTime.getTime() < DateUtil.date().getTime()) {
            list.add(DateUtil.format(dateTime, "yyyy-MM-dd"));
            dateTime = DateUtil.offsetDay(dateTime, 1);
        }
        return list;
    }

}
